package com.dhcc.DRGsHosp.infra.util.sax;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.codec.binary.Base64;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.dhcc.DRGsHosp.infra.util.StringUtils;

public class SaxService {
 
    public static List<Map<String, Object>> ReadXML(String xml) throws ParserConfigurationException, SAXException, IOException{
    	boolean hasBaseXml = false;
    	boolean isBase64 = false;
    	List<Map<String, Object>> mrList = new LinkedList<>();
    	InputStream propInputStream = null; //后面改成全局变量, 采用页面配置, 别忘了
		Properties properties = null;
    	propInputStream = SaxService.class.getResourceAsStream("/mr_tags.properties");
		properties = new Properties();
		try {
			properties.load(propInputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
    	//获取isXmlContent的值
    	hasBaseXml = properties.get("hasBaseXml")!=null?("1".equals(properties.get("hasBaseXml"))?true:false):false;
    	isBase64 = properties.get("xmlBase64")!=null?("1".equals(properties.get("xmlBase64"))?true:false):false;
    	List<List<Map<String, String>>> list = null;
        //创建一个解析XML的工厂对象
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        //创建一个解析XML的对象
        SAXParser parser = parserFactory.newSAXParser();
        //创建一个解析助手类
        StringReader read = new StringReader(xml);
        InputSource source = new InputSource(read);
        Myhandler myhandler;
        if(hasBaseXml){
        	myhandler = new Myhandler(properties.getProperty("xmlBase"));
        	parser.parse(source, myhandler);
            List<List<Map<String, String>>> resultList = myhandler.getList();
            String xmlBaseContentTag = properties.getProperty("xmlBaseContent");
            if(xmlBaseContentTag == null) xmlBaseContentTag="";
            if(resultList.size()>0){
            	List<Map<String, String>> rList = resultList.get(0);
            	String xmlStr = "";
            	for(Map<String,String> m: rList){
            		if(xmlBaseContentTag.equals(m.get("tags"))){
            			xmlStr = m.get("tagValue");
            		}
            	}
            	if(StringUtils.hasLength(xmlStr)){
            		if(isBase64){
            			xmlStr = new String(Base64.decodeBase64(xmlStr.getBytes()));
            			xml = xmlStr;
            		}
            	}
            }
        }
		read = new StringReader(xml);
        source = new InputSource(read);
        String xmlContentTag = properties.getProperty("docuemnt"); //首页
        String xmlMainDiagnoseTag = properties.getProperty("PDM");//主要诊断子节点
        String xmlOtherDiagnoseTag = properties.getProperty("PDO");//主要诊断子节点
        String xmlMainOperationTag = properties.getProperty("POM");//主要诊断子节点
        String xmlOtherOperationTag = properties.getProperty("POO");//主要诊断子节点
        String xmlCostTag = properties.getProperty("PE");
		myhandler = new Myhandler(xmlContentTag);
		parser.parse(source, myhandler);
		list = myhandler.getList();
		for(List<Map<String, String>> lis: list){
			Map<String, Object> mrMap = new HashMap<>();
			List<Map<String, String>> diagnosesM = new LinkedList<>();//主要诊断信息
			List<Map<String, String>> diagnosesO = new LinkedList<>();//其他诊断信息
			List<Map<String, String>> operationsM = new LinkedList<>();//主要手术信息
			List<Map<String, String>> operationsO = new LinkedList<>();//其他手术信息
			List<Map<String, String>> costs = new LinkedList<>();//费用
			for(Map<String, String> ma: lis){
				if(ma.get("tags").contains(xmlMainDiagnoseTag)){
					diagnosesM.add(ma);
					continue;
				}
				if(ma.get("tags").contains(xmlOtherDiagnoseTag)){
					diagnosesO.add(ma);
					continue;
				}
				if(ma.get("tags").contains(xmlMainOperationTag)){
					operationsM.add(ma);
					continue;
				}
				if(ma.get("tags").contains(xmlOtherOperationTag)){
					operationsO.add(ma);
					continue;
				}
				if(ma.get("tags").contains(xmlCostTag)){
					costs.add(ma);
					continue;
				}
				String mk = properties.getProperty(ma.get("tags"));
				if(mk!=null){
					mrMap.put(mk, ma.get("tagValue"));
				}
			}
			//处理手术和诊断
			List<Map<String, Object>> diagnosesList = new LinkedList<>();
			String firstTagForList = null;
			Map<String, Object> m = null;
			for(int i=0; i<diagnosesM.size(); i++){
				if(i==0){
					firstTagForList = "";
					m = new HashMap<>();
					m.put("PDType", "M");
				}
				if(firstTagForList.contains(diagnosesM.get(i).get("tags"))){
					diagnosesList.add(m);
					m = new HashMap<>();
					m.put("PDType", "M");
					firstTagForList = "";
				}else{
					firstTagForList += diagnosesM.get(i).get("tags")+",";
				}
				Map<String, String> m1 = diagnosesM.get(i);
				String mk = properties.getProperty(m1.get("tags"));
				if(mk!=null){
					m.put(mk, m1.get("tagValue"));
				}
				if(i==diagnosesM.size()-1){
					diagnosesList.add(m);
				}
			}
			for(int i=0; i<diagnosesO.size(); i++){
				if(i==0){
					firstTagForList = "";
					m = new HashMap<>();
					m.put("PDType", "O");
				}
				if(firstTagForList.contains(diagnosesO.get(i).get("tags"))){
					diagnosesList.add(m);
					m = new HashMap<>();
					m.put("PDType", "O");
					firstTagForList = "";
				}
				firstTagForList += diagnosesO.get(i).get("tags")+",";
				Map<String, String> m1 = diagnosesO.get(i);
				String mk = properties.getProperty(m1.get("tags"));
				if(mk!=null){
					m.put(mk, m1.get("tagValue"));
				}
				if(i==diagnosesO.size()-1){
					diagnosesList.add(m);
					m = new HashMap<>();
					firstTagForList = "";
				}
			}
			mrMap.put("diagnoses", diagnosesList);
			//处理手术和诊断
			List<Map<String, Object>> operationList = new LinkedList<>();
			for(int i=0; i<operationsM.size(); i++){
				if(i==0){
					firstTagForList = "";
					m = new HashMap<>();
					m.put("POType", "M");
				}
				if(firstTagForList.contains(operationsM.get(i).get("tags"))){
					operationList.add(m);
					m = new HashMap<>();
					m.put("POType", "M");
					firstTagForList = "";
				}else{
					firstTagForList += operationsM.get(i).get("tags")+",";
				}
				Map<String, String> m1 = operationsM.get(i);
				String mk = properties.getProperty(m1.get("tags"));
				if(mk!=null){
					m.put(mk, m1.get("tagValue"));
				}
				if(i==operationsM.size()-1){
					operationList.add(m);
				}
			}
			for(int i=0; i<operationsO.size(); i++){
				if(i==0){
					firstTagForList = "";
					m = new HashMap<>();
					m.put("POType", "O");
				}
				if(firstTagForList.contains(operationsO.get(i).get("tags"))){
					operationList.add(m);
					m = new HashMap<>();
					m.put("POType", "O");
					firstTagForList = "";
				}else{
					firstTagForList += operationsO.get(i).get("tags")+",";
				}
				Map<String, String> m1 = operationsO.get(i);
				String mk = properties.getProperty(m1.get("tags"));
				if(mk!=null){
					m.put(mk, m1.get("tagValue"));
				}
				if(i==operationsO.size()-1){
					operationList.add(m);
					m = new HashMap<>();
					firstTagForList = "";
				}
			}
			mrMap.put("operations", operationList);
			//处理费用
			List<Map<String, Object>> costList = new LinkedList<>();
			for(Map<String, String> m1: costs){
				m = new HashMap<>();
				String mk = properties.getProperty(m1.get("tags"));
				String eCode = mk.replace("PE", "");
				int code = Integer.parseInt(eCode);
//					if(mk!=null){
					m.put("PE0101", code);
					m.put("PE0200", m1.get("tagValue"));
					costList.add(m);
//					}
			}
			mrMap.put("costs", costList);
			mrList.add(mrMap);
		}
        return mrList;
    }
}